<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * FormativeLab
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		FormativeLab
 * @author		FormativeLab Dev Team
 * @copyright	Copyright (c) 2011 - 2013, FormativeLab, Inc.
 * @license		http://www.formativesoft.com/license/
 * @link		http://www.formativesoft.com/
 * @since		Version 2.0
 * @filesource ./application/helpers/model_helper.php
 */

// ------------------------------------------------------------------------

/**
 * FormativeLab Method Helpers
 *
 * @package		FormativeLab
 * @subpackage	Helpers
 * @category	Helpers
 * @author		Formative Soft Ltd
 * @link		http://www.formativesoft.com/
 */

// ------------------------------------------------------------------------

/**
 * isGet() Cheack either the request method is GET or not
 *
 * @access	public
 * @return	bool
 */
if (!function_exists('isGet')){
	function isGet(){		   
        return ($_SERVER['REQUEST_METHOD'] === 'GET');
	}
}

// ------------------------------------------------------------------------

/**
 * isPost() Cheack either the request method is POST or not
 *
 * @access	public
 * @return	bool
 */
if (!function_exists('isPost')){
	function isPost(){		   
        return ($_SERVER['REQUEST_METHOD'] === 'POST');
	}
}

// ------------------------------------------------------------------------

/**
 * isPut() Cheack either the request method is PUT or not
 *
 * @access	public
 * @return	bool
 */
if (!function_exists('isPut')){
	function isPut(){		   
        return ($_SERVER['REQUEST_METHOD'] === 'PUT');
	}
}

// ------------------------------------------------------------------------

/**
 * isDelete() Cheack either the request method is PUT or not
 *
 * @access	public
 * @return	bool
 */
if (!function_exists('isDelete')){
	function isDelete(){		   
        return ($_SERVER['REQUEST_METHOD'] === 'DELETE');
	}
}

// ------------------------------------------------------------------------

/**
 * isAjaxRequest() Cheack either the AJAX request or not
 *
 * @access	public
 * @return	bool
 */
if (!function_exists('isAjaxRequest')){
	function isAjaxRequest(){
		$controller = &get_instance();
		return $controller->input->is_ajax_request();
	}
}

// ------------------------------------------------------------------------

/**
 * jsonRequest() read json request
 *
 * @access	public
 * @param   assoc
 * @param   depth
 * @return	mixed
 */
if (!function_exists('jsonRequest')){
	function jsonRequest($assoc=false, $depth=512){
		if((isPostRequest()||isPutRequest()||isDeleteRequest())&&isAjaxRequest()){
			$standardInput	= 'php://input';
			$requestBody	= file_get_contents($standardInput);
			$phpObject		= json_decode(trim($requestBody), $assoc, $depth);
			return $phpObject;
		}
	}
}

// ------------------------------------------------------------------------

/**
 * jsonRequestIsObject() read json request
 *
 * @access	public
 * @param   assoc
 * @param   depth
 * @return	list
 */
if (!function_exists('jsonRequestIsObject')){
	function jsonRequestIsObject($assoc=false, $depth=512){
		$rawdata	= jsonRequest($assoc, $depth);
		$data		= $rawdata->data;
		return is_object($data);
	}
}

// ------------------------------------------------------------------------

/**
 * jsonRequestIsArray() read json request
 *
 * @access	public
 * @param   assoc
 * @param   depth
 * @return	list
 */
if (!function_exists('jsonRequestIsArray')){
	function jsonRequestIsArray($assoc=false, $depth=512){
		$rawdata	= jsonRequest($assoc, $depth);
		$array		= $rawdata->data;
		return is_array($array);
	}
}

// --------------------------------------------------------------------

/**
 * jsonRequestDecode() - Contains two mandatory input parameters. First one is
 * entity and last one is Array 
 *
 * Returns array of Standard Model Object
 *
 * @access	public
 * @param	entity
 * @param	data
 * @return	object
 */

if (!function_exists('jsonRequestDecode')){
	function jsonRequestDecode(&$entity, $data){
		$model 		= null;
		$clazz		= get_class($entity);

		if(mappingPropertyExists($clazz)){
			if(isset($data)){
				$model 		= new $clazz();
				$mapping 	= $model->getMapping();
				foreach($mapping as $property => $metaData){
					$dtype = $metaData['jstype'];
					if(array_key_exists($property, $data)){
						if(isset($data->{$property}) && !isLogicalNullKey($metaData, $data->{$property})){
							switch($dtype){								
								case 'int'		: $model->{$property} = intval($data->{$property}); 	break;
								case 'auto'		: $model->{$property} = strval($data->{$property});		break;
								case 'date'		: $model->{$property} = parseDate($data->{$property}); 	break;
								case 'float'	: $model->{$property} = doubleval($data->{$property}); 	break;								
								case 'string'	: $model->{$property} = trimString($data->{$property});	break;
								case 'boolean'	: $model->{$property} = boolval($data->{$property}); 	break;								
							}
						}
					}			 
				}
			}
		}else{
			$message = sprintf('Undefined static property [mapping] in class %s', $clazz);
			trigger_error($message, E_USER_ERROR);
		}
		return $model;
	}
}

// ------------------------------------------------------------------------

/**
 * jsonRequestAsModel() read json request
 *
 * @access	public
 * @param   clazz
 * @param   assoc
 * @param   depth
 * @return	object
 */
if (!function_exists('jsonRequestAsModel')){
	function jsonRequestAsModel($clazz, $assoc=false, $depth=512){
		$entity 		= new $clazz();
		$rawdata		= jsonRequest($assoc, $depth);
		$data			= $rawdata->data;
		if(jsonRequestIsObject($assoc, $depth)){
			return jsonRequestDecode($entity, $data);
		}
	}
}

// ------------------------------------------------------------------------

/**
 * jsonRequestAsList() read json request
 *
 * @access	public
 * @param   clazz
 * @param   assoc
 * @param   depth
 * @return	list
 */
if (!function_exists('jsonRequestAsList')){
	function jsonRequestAsList($clazz, $assoc=false, $depth=512){
		$entity 		= new $clazz();
		$rawdata		= jsonRequest($assoc, $depth);
		$array			= $rawdata->data;
		if(jsonRequestIsObject($assoc, $depth)){
			foreach($array as $index => $data){
				$list[] = jsonRequestDecode($entity, $data);
			}
			return $list;
		}
	}
}

// ------------------------------------------------------------------------

/**
 * jsonRender() use for json response
 *
 * @access	public
 * @param   message
 * @param   success
 * @param   status
 * @return	void
 */
if (!function_exists('jsonRender')){
	function jsonRender($message = '', $success = true, $status = 200){
		$response = array(
			'success' => $success,
			'message' => $message
		);
		jsonDataRender($response, $status);
	}
}

// ------------------------------------------------------------------------

/**
 * jsonDataRender() use for json response
 *
 * @access	public
 * @param   data
 * @param   status
 * @return	void
 */
if (!function_exists('jsonDataRender')){
	function jsonDataRender(Array $data = array(), $status = 200){
		$jsonData 	= json_encode($data);
		$controller = &get_instance();
		$controller->output->set_status_header($status);
		$controller->output->set_header('Content-type: application/json; charset=utf-8');
		$controller->output->set_output($jsonData);
	}
}

// ------------------------------------------------------------------------

/**
 * htmlRender() use for json response
 *
 * @access	public
 * @param   message
 * @param   success
 * @param   status
 * @return	void
 */
if (!function_exists('htmlRender')){
	function htmlRender($message = '', $success = true, $status = 200){
		$response = array(
			'success' => $success,
			'message' => $message
		);
		$jsonData 	= json_encode($response);
		$controller = &get_instance();
		$controller->output->set_status_header($status);
		$controller->output->set_header('Content-type: text/html; charset=utf-8');
		$controller->output->set_output($jsonData);
	}
}

// ------------------------------------------------------------------------

/**
 * notAjaxGetRender() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('notAjaxGetRender')){
	function notAjaxGetRender(){
		$message	= 'Access Restrict, Invalid Request!';
		$success	= false;
		$status		= 200;
		if(!isAjaxRequest() || !isGet()){
			htmlRender($message, $success, $status);
		}
	}
}

// ------------------------------------------------------------------------

/**
 * notAjaxPostRender() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('notAjaxPostRender')){
	function notAjaxPostRender(){
		$message	= 'Access Restrict, Invalid Request!';
		$success	= false;
		$status		= 200;
		if(!isAjaxRequest() || !isPost()){
			htmlRender($message, $success, $status);
		}
	}
}

// ------------------------------------------------------------------------

/**
 * notAjaxPutRender() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('notAjaxPutRender')){
	function notAjaxPutRender(){
		$message	= 'Access Restrict, Invalid Request!';
		$success	= false;
		$status		= 200;
		if(!isAjaxRequest() || !isPut()){
			htmlRender($message, $success, $status);
		}
	}
}

// ------------------------------------------------------------------------

/**
 * notAjaxDeleteRender() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('notAjaxPutRender')){
	function notAjaxDeleteRender(){
		$message	= 'Access Restrict, Invalid Request!';
		$success	= false;
		$status		= 200;
		if(!isAjaxRequest() || !isDelete()){
			htmlRender($message, $success, $status);
		}
	}
}

// ------------------------------------------------------------------------

/**
 * notGetRender() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('notGetRender')){
	function notGetRender(){
		$message	= 'Access Restrict, Invalid Request!';
		$success	= false;
		$status		= 200;
		if(!isGet()){
			if(isAjaxRequest()){
				jsonRender($message, $success, $status);
			}else{
				htmlRender($message, $success, $status);
			}
		}
	}
}

// ------------------------------------------------------------------------

/**
 * notPostRender() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('notPostRender')){
	function notPostRender(){
		$message	= 'Access Restrict, Invalid Request!';
		$success	= false;
		$status		= 200;
		if(!isPost()){
			if(isAjaxRequest()){		
				jsonRender($message, $success, $status);
			}else{
				htmlRender($message, $success, $status);
			}
		}
	}
}

// ------------------------------------------------------------------------

/**
 * notPutRender() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('notPutRender')){
	function notPutRender(){
		$message	= 'Access Restrict, Invalid Request!';
		$success	= false;
		$status		= 200;
		if(!isPut()){
			if(isAjaxRequest()){		
				jsonRender($message, $success, $status);
			}else{
				htmlRender($message, $success, $status);
			}
		}
	}
}

// ------------------------------------------------------------------------

/**
 * notDeleteRender() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('notDeleteRender')){
	function notDeleteRender(){
		$message	= 'Access Restrict, Invalid Request!';
		$success	= false;
		$status		= 200;
		if(!isDelete()){
			if(isAjaxRequest()){		
				jsonRender($message, $success, $status);
			}else{
				htmlRender($message, $success, $status);
			}
		}
	}
}

// ------------------------------------------------------------------------

/**
 * isGetRequest() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('isGetRequest')){
	function isGetRequest(){
		$isGetRequest = isGet();
		if(!$isGetRequest){
			notGetRender();
		}
		return $isGetRequest;
	}
}

// ------------------------------------------------------------------------

/**
 * isPostRequest() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('isPostRequest')){
	function isPostRequest(){
		$isPostRequest = isPost();
		if(!$isPostRequest){
			notPostRender();
		}
		return $isPostRequest;
	}
}

// ------------------------------------------------------------------------

/**
 * isPutRequest() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('isPutRequest')){
	function isPutRequest(){
		$isPutRequest = isPut();
		if(!$isPutRequest){
			notPutRender();
		}
		return $isPutRequest;
	}
}

// ------------------------------------------------------------------------

/**
 * isDeleteRequest() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('isDeleteRequest')){
	function isDeleteRequest(){
		$isDeleteRequest = isDelete();
		if(!$isDeleteRequest){
			notDeleteRender();
		}
		return $isDeleteRequest;
	}
}

// ------------------------------------------------------------------------

/**
 * isAjaxGetRequest() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('isAjaxGetRequest')){
	function isAjaxGetRequest(){
		$isAjaxGetRequest = isAjaxRequest() && isGet();
		if(!$isAjaxGetRequest){
			notAjaxGetRender();
		}
		return $isAjaxGetRequest;
	}
}

// ------------------------------------------------------------------------

/**
 * isAjaxPostRequest() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('isAjaxPostRequest')){
	function isAjaxPostRequest(){
		$isAjaxPostRequest = isAjaxRequest() && isPost();
		if(!$isAjaxPostRequest){
			notAjaxPostRender();
		}
		return $isAjaxPostRequest;
	}
}

// ------------------------------------------------------------------------

/**
 * isAjaxPutRequest() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('isAjaxPutRequest')){
	function isAjaxPutRequest(){
		$isAjaxPutRequest = isAjaxRequest() && isPut();
		if(!$isAjaxPutRequest){
			notAjaxPutRender();
		}
		return $isAjaxPutRequest;
	}
}

// ------------------------------------------------------------------------

/**
 * isAjaxDeleteRequest() for restrict access of others request method
 *
 * @access	public
 * @return	void
 */
if (!function_exists('isAjaxDeleteRequest')){
	function isAjaxDeleteRequest(){
		$isAjaxDeleteRequest = isAjaxRequest() && isDelete();
		if(!$isAjaxDeleteRequest){
			notAjaxDeleteRender();
		}
		return $isAjaxDeleteRequest;
	}
}


/* End of file model_helper.php */
/* Location: ./application/helpers/model_helper.php */